package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.Travelcontent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IMessageService;
import cn.wolfcode.trip.base.service.ITravelContentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 游记资源
 */
@Controller
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelContentService travelContentService;
    @Autowired
    private IMessageService messageService;

    /**
     * 待审核游记
     * @param qo
     * @return
     */
    @RequestMapping("/audit_list")
    public String auditList(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        //设置为待审核状态
        if(qo.getState() == null){
            qo.setState(Travel.STATE_AUDIT);
        }

        //公开状态
        qo.setIsPublic(true);
        //排序规则
        qo.setOrderBy("lastUpdateTime");

        model.addAttribute("pageInfo",travelService.query(qo));
        return "/travel/audit_list";
    }

    /**
     * 已发布游记
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        qo.setState(2);
        //公开状态
        qo.setIsPublic(true);
        //排序规则
        qo.setOrderBy("releaseTime DESC");

        model.addAttribute("pageInfo",travelService.query(qo));
        return "/travel/list";
    }

    /**
     * 修改状态
     * @param travel : id+state (发布时间)
     * @return
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public JsonResult changeState(Travel travel){
        JsonResult jsonResult=new JsonResult();
        try {
            travelService.changeState(travel);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }

        //查询当前游记
        Travel currentTravel = travelService.get(travel.getId());

        //在发布或者拒绝的时候,存入消息内容,用于发送给用户
        Message message=new Message();
        //用户id
        message.setReceiver(currentTravel.getAuthor());
        //系统通知类型
        message.setType(4);
        //设置消息未读状态
        message.setState(0);
        if(travel.getState()==2){
            message.setContent("亲,您写的游记发布成功~");
        }
        if(travel.getState()==-1){
            message.setContent("亲,您写的游记已被拒绝发布,请修改后再提交~");
        }
        //游记
        message.setTravel(travel);
        //发送时间
        message.setSendTime(new Date());

        //保存消息数据到数据库中
        messageService.save(message);
        return jsonResult;
    }

    /**
     * 查阅游记文章文本内容
     * @param id : 游记文章的id
     * @return
     */
    @RequestMapping("/read")
    @ResponseBody
    public Travelcontent getContent(Long id){
        return travelContentService.get(id);
    }
}
